<?php

namespace frontend\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use frontend\models\Event;
use frontend\models\EventSearch;
use frontend\models\EventType;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;


class EventController extends Controller
{
    /**
     * Lists all News models.
     * @return mixed
     */
    public function actionIndex()
    {
        $eventsData = EventType::find()
                    ->joinWith([
                        'events' => function ($query) {
                            $query->andWhere(['>=', 'event.date_end', date('Y-m-d')])
                            ->andWhere('event.id_info_chanel=2')
                            ->orderBy('event.date_start');

                        },
                    ])
                    ->where('event.is_main_event=0')
                    ->andWhere(['>=', 'event.date_end', date('Y-m-d')])
                    ->andWhere('event.id_info_chanel=2')
                    ->asArray()
                    ->all();

        $mainEvents = Event::find()
                    ->where('event.is_main_event=1')
                    ->andWhere(['>=', 'date_start', date('Y-m-d')])
                    ->andWhere('event.id_info_chanel=2')
                    ->orderBy('date_start')
                    ->asArray()
                    ->all();
                    
        $this->layout = 'common';

        return $this->render('index', [
            'mainEvents' => $mainEvents,
            'eventsData' => $eventsData,
        ]);
    }

    /**
     * Displays a single Event model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        $eventModel = $this->findModel($id);

        $eventDataProvider = new ActiveDataProvider([
            'query' => Event::find()->where(['!=', 'id', $eventModel->id])->orderBy(['date_public' => SORT_ASC])->limit(3),
            'pagination' => false,
        ]);

        $this->layout = 'common';
        
        return $this->render('view', [
            'eventDataProvider' => $eventDataProvider,
            'model' => $eventModel,
        ]);
    }


    /**
     * Finds the News model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return News the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Event::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
